package com.boarsoft.boar.code.mac.flow.biz.impl;

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.code.entity.ProHandlerInfo;
import com.boarsoft.boar.code.mac.flow.biz.ProcessHandlerInfoBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("codeBiz")
public class ProcessHandlerInfoBizImpl extends SimpleBizImpl implements ProcessHandlerInfoBiz {
	@Override
	@Transactional
	public ReplyInfo<String> delete(String id) {
		return null;
	}

	@Override
	@Transactional
	public ReplyInfo<Object> update(ProHandlerInfo d) {
		return null;
	}

	@Override
	@Transactional
	public String add(ProHandlerInfo f) {
		try{
			this.save(f);
			return f.getId();
		} catch (DataIntegrityViolationException e) {
			log.error("保存失败",e);
			return null;
		}
	}

	@Override
	@Transactional(readOnly = true)
	public PagedResult<ProHandlerInfo> list(String appId,String faceId, String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from ").append(ProHandlerInfo.class.getName());
		sb.append(" s where 1=1");
		if(Util.strIsNotEmpty(appId)) {
			sb.append(" and s.appId = '").append(appId).append("'");
		}
		if(Util.strIsNotEmpty(faceId)) {
			sb.append(" and s.faceId = '").append(faceId).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (s.code like '%").append(key);
			sb.append("%' or s.faceCode like '%").append(key);
			sb.append("%' or s.beanId like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal("select count(s.id)".concat(sb.toString()));
		// if (Util.strIsNotEmpty(orderBy)) {
		// sb.append(" order by ").append(orderBy);
		// } else {
		// sb.append(" order by name");
		// }
		List<ProHandlerInfo> lt = dao.list("select s".concat(sb.toString()), pageNo, pageSize);
		return new PagedResult<>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public ProHandlerInfo get(String code, String appId) {
		StringBuilder sb = new StringBuilder();
		sb.append("and vCode='").append(code).append("'");
		sb.append("and vAppId='").append(appId).append("'");
		return dao.find(ProHandlerInfo.class, sb.toString());
	}

	@Override
	@Transactional
	public void save(ProHandlerInfo o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
	}
}